/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.util.webui;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author MU
 */
public class ResourcesHelper {

    public static enum DocumentType {WORD, EXCEL, NOT_SUPPORTED}

    private static final String WORD_POSTFIX = ".doc";

    private static final String EXCEL_POSTFIX = ".xls";

    public static String getRealPath() {
        ServletContext servletContext = ((HttpServletRequest)FacesContext
                    .getCurrentInstance()
                    .getExternalContext()
                    .getRequest())
                    .getSession()
                    .getServletContext();
        return servletContext.getRealPath("/");
    }

    public static String getUploadDirectory() {
        // need to be configurable
        return "C:/web/rec/web/upload";
    }

    public static DocumentType getFileType(String fileName) {
        String postfix = fileName.substring(fileName.lastIndexOf('.'));
        if (postfix != null && postfix.equals(WORD_POSTFIX)) {
            return DocumentType.WORD;
        } else if (postfix != null && postfix.equals(EXCEL_POSTFIX)) {
            return DocumentType.EXCEL;
        } else {
            return DocumentType.NOT_SUPPORTED;
        }
    }
    
}
